Passed
Pull Request — master (#17)
by
unknown
01:03
created

options.js ➔ ... ➔ ???   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
/* eslint-env browser, jquery */
2
3
// Displays message to user
4
function showStatus(message) {
5
  const $status = document.getElementById('status');
6
  $status.innerHTML = message;
7
  setTimeout(() => {
8
    $status.innerHTML = '';
9
  }, 1750);
10
}
11
12
// Saves options to localStorage.
13
function saveOptions() {
14
  const $useMaildrop = $('#use-maildrop');
15
  const $address = $('#maildrop-address');
16
17
  $address.val($address.val().replace(/^\s*/, '').replace(/\s*$/, ''));
18
  const address = $address.val();
19
  const isAddressValid = address.match(/^[a-z.-_]+$/i);
20
21
  if (isAddressValid) {
22
    $address.removeClass('invalid');
23
    localStorage.address = address;
0 ignored issues
show
Bug introduced by
The variable localStorage seems to be never declared. If this is a global, consider adding a /** global: localStorage */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
24
  } else {
25
    $useMaildrop.prop('checked', false);
26
    $address.addClass('invalid');
27
    localStorage.sender = 'app';
28
  }
29
30
  const isMailDropEnabled = $useMaildrop.prop('checked');
31
  localStorage.sender = (isMailDropEnabled && isAddressValid) ? 'maildrop' : 'app';
32
33
  // Update status to let user know options were saved.
34
  showStatus('Options saved.');
35
}
36
37
// Restores select box state to saved value from localStorage.
38
function restoreOptions() {
39
  document.getElementById('use-maildrop').checked = (localStorage.sender === 'maildrop');
0 ignored issues
show
Bug introduced by
The variable localStorage seems to be never declared. If this is a global, consider adding a /** global: localStorage */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
40
  document.getElementById('maildrop-address').value = localStorage.address || '';
41
}
42
43
function resetOptions() {
44
  document.getElementById('maildrop-address').value = '';
45
  document.getElementById('use-maildrop').checked = false;
46
  saveOptions();
47
}
48
49
document.addEventListener('DOMContentLoaded', restoreOptions);
50
document.querySelector('#save').addEventListener('click', saveOptions);
51
document.querySelector('#reset').addEventListener('click', resetOptions);
52